$(function()
{
    var bWizardTabClass = '';
    $('.wizard').each(function()
    {
        if ($(this).is('#rootwizard'))
            bWizardTabClass = 'bwizard-steps';
        else
            bWizardTabClass = '';

        var wiz = $(this);

        $(this).bootstrapWizard(
                {
                    onNext: function(tab, navigation, index)
                    {
                        if (index == 1)
                        {
                            // Make sure we entered the title
//                            if (wiz.find('#po_no_no').val() == 0) {
//                                alert('You must select PO number');
//                                wiz.find('#po_no_no').focus();
//                                return false;
//                            }
                        }

                    },
                    onLast: function(tab, navigation, index)
                    {
                        // Make sure we entered the title
//                        if (wiz.find('#po_no_no').val() == 0) {
//                            alert('You must select PO number');
//                            wiz.find('#po_no_no').focus();
//                            return false;
//                        }
                    },
                    onTabClick: function(tab, navigation, index)
                    {
//                        if (wiz.find('#po_no_no').val() == 0) {
//                            alert('You must select PO number');
//                            wiz.find('#po_no_no').focus();
//                            return false;
//                        }
                    },
                    onTabShow: function(tab, navigation, index)
                    {
                        var $total = navigation.find('li:not(.status)').length;
                        var $current = index + 1;
                        var $percent = ($current / $total) * 100;

                        if (wiz.find('.progress-bar').length)
                        {
                            wiz.find('.progress-bar').css({width: $percent + '%'});
                            wiz.find('.progress-bar')
                                    .find('.step-current').html($current)
                                    .parent().find('.steps-total').html($total)
                                    .parent().find('.steps-percent').html(Math.round($percent) + "%");
                        }

                        // update status
                        if (wiz.find('.step-current').length)
                            wiz.find('.step-current').html($current);
                        if (wiz.find('.steps-total').length)
                            wiz.find('.steps-total').html($total);
                        if (wiz.find('.steps-complete').length)
                            wiz.find('.steps-complete').html(($current - 1));

                        // mark all previous tabs as complete
                        navigation.find('li:not(.status)').removeClass('primary');
                        navigation.find('li:not(.status):lt(' + ($current - 1) + ')').addClass('primary');

                        // If it's the last tab then hide the last button and show the finish instead
                        if ($current >= $total) {
                            wiz.find('.pagination .next').hide();
                            wiz.find('.pagination .finish').show();
                            wiz.find('.pagination .finish').removeClass('disabled');
                        } else {
                            wiz.find('.pagination .next').show();
                            wiz.find('.pagination .finish').hide();
                        }
                    },
                    tabClass: bWizardTabClass,
                    nextSelector: '.next',
                    previousSelector: '.previous',
                    firstSelector: '.first',
                    lastSelector: '.last'
                });

        wiz.find('.finish').click(function()
        {
            var project_code = $("#project_code_orderPage").val();
            var project_order_status = $("#project_order_status").val();
            var project_home_plan = $("#project_plan").val();
            var project_home_plot = $("#project_plot").val();
            var project_document_no = $("#project_document_no").val();
            var project_po_no = $("#project_po_no_name").val();
            var project_po_owner = $("#project_po_owner").val();
            var project_po_sender = $("#project_po_sender").val();
            var project_order_type = $("#project_order_type").val();
            var project_plan_size = $("#project_plan_size").val();
            var project_unit_price = $("#project_unit_price").val();
            var project_amount = $("#project_amount").val();
            var project_vat = $("#project_vat").val();
            var poid = $("#project_po_no_id").val();
            var poForEdit = $("#po_no_no").val();
            var project_order_remark = $("#project_order_remark").val();
            var order_id = $("#project_oid").val();

            var wo_team_code = $("#wo_team_code").val();
            var teamCode = $("#wo_team_code").val();
            var wo_assign_date = $("#wo_assign_date").val();
            var wo_target_date = $("#wo_target_date").val();
            var wo_remark = $("#wo_remark").val();
            /*
             * Edit State for order page
             */
            if (initialState === "EditState") {
                if (project_order_status === "Assign") {
                    if (wo_team_code == 0) {
                        $("#team_code_error").addClass("has-error");
                    } else {
                        $("#team_code_error").removeClass("has-error");
//                        alert(project_order_status);
                        var jqxhr = $.post("model/SavingAssignDetail.php?order_id=" + order_id +
                                "&project_id=" + project_code +
                                "&team_code=" + teamCode +
                                "&assign_date" + wo_assign_date +
                                "&target_date=" + wo_target_date +
                                "&project_order_status =" + project_order_status +
                                "&project_order_remark=" + project_order_remark +
                                "&remark=" + wo_remark);
                        jqxhr.success(function(data) {
                            wiz.find("a[data-toggle*='tab']:first").trigger('click');
                            $("#modal-project-order").modal('hide');
                            setTimeout(function()
                            {
                                $("#load_project_order").load("ProjectOrderPage.php?project_code=" + project_code);

                            }
                            , 500);
                            if (data == 1) {
                                $.gritter.add({
                                    title: 'Success!',
                                    text: "แก้ไขสำเร็จแล้ว"
                                });
                            }
                        });
                        jqxhr.error(function(data) {
                            window.location.replace("error.php?error_msg=" + data);
                        });
                    }
                } else {
                    if (project_order_remark === "") {
                        alert("กรุณาใส่ Remark !");
                    } else {
                        var jqxhr = $.post("model/EditProjectOrder.php?project_code=" + project_code +
                                "&project_order_status=" + project_order_status +
                                "&project_home_plan=" + project_home_plan +
                                "&project_home_plot=" + project_home_plot +
                                "&project_document_no=" + project_document_no +
                                "&project_po_no=" + project_po_no +
                                "&project_po_owner=" + project_po_owner +
                                "&project_po_sender=" + project_po_sender +
                                "&project_order_type=" + project_order_type +
                                "&project_plan_size=" + project_plan_size +
                                "&project_unit_price=" + project_unit_price +
                                "&project_amount=" + project_amount +
                                "&project_vat=" + project_vat +
                                "&order_id=" + order_id +
                                "&project_image_path=" + poForEdit +
                                "&project_remark=" + project_order_remark);
                        jqxhr.success(function(data) {
                            wiz.find("a[data-toggle*='tab']:first").trigger('click');
                            $("#modal-project-order").modal('hide');
                            setTimeout(function()
                            {
                                $("#load_project_order").load("ProjectOrderPage.php?project_code=" + project_code);

                            }
                            , 500);
                            if (data == 1) {
                                $.gritter.add({
                                    title: 'Success!',
                                    text: "แก้ไขสำเร็จแล้ว"
                                });
                            }
                        });
                        jqxhr.error(function(data) {
                            window.location.replace("error.php?error_msg=" + data);
                        });
                    }
                }
            } else {
                /*
                 * New State for order page
                 */
                var jqxhr = $.post("model/SavingProjectOrder.php?project_code=" + project_code +
                        "&project_order_status=" + project_order_status +
                        "&project_home_plan=" + project_home_plan +
                        "&project_home_plot=" + project_home_plot +
                        "&project_document_no=" + project_document_no +
                        "&project_po_no=" + project_po_no +
                        "&project_po_owner=" + project_po_owner +
                        "&project_po_sender=" + project_po_sender +
                        "&project_order_type=" + project_order_type +
                        "&project_plan_size=" + project_plan_size +
                        "&project_unit_price=" + project_unit_price +
                        "&project_amount=" + project_amount +
                        "&project_image_path=" + poForEdit +
                        "&project_vat=" + project_vat);
                jqxhr.success(function(data) {
                    wiz.find("a[data-toggle*='tab']:first").trigger('click');
                    $("#modal-project-order").modal('hide');
                    setTimeout(function()
                    {
                        $("#load_project_order").load("ProjectOrderPage.php?project_code=" + project_code);

                    }
                    , 500);
                    if (data == 1) {
                        $.gritter.add({
                            title: 'Success!',
                            text: "บันทึกสำเร็จแล้ว"
                        });
                    } else {
                        alert(data);
                    }
                });
                jqxhr.error(function(data) {
                    window.location.replace("error.php?error_msg=" + data);
                });
            }

        });
    });
});